﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TjZheng.Framework;
using TjZheng.JiaLi.Process;
using TjZheng.Logging;
using TjZheng.Logging.Service;

namespace TjZheng.JiaLi.Winform
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //处理未捕获的异常   
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常   
            Application.ThreadException += Application_ThreadException;
            //处理非UI线程异常   
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            InvokeResult objInvokeResult = TicketInfo.CheckTicket();
            if (objInvokeResult.ResultCode == "Success")
            {
                objInvokeResult = TicketInfo.CheckIsActive();
                if (objInvokeResult.ResultCode == "Success")
                {
                    if (Convert.ToString(objInvokeResult.Data) == "true")
                    {
                        TicketForm objTicketForm = new JiaLiForm1_1();
                        ShowMessage = objTicketForm.ShowMessage;
                        Application.Run(objTicketForm);
                    }
                    else
                    {
                        objInvokeResult.ResultCode = "ShowActive";
                        objInvokeResult.ResultCode = "尚未激活，弹出激活窗口";
                        objInvokeResult.Data = null;
                    }
                }
            }
            if (objInvokeResult.ResultCode != "Success")
            {
                ActivatedForm objRegisterForm = new ActivatedForm();
                objRegisterForm.ShowDialog();
                if (objRegisterForm.DialogResult == DialogResult.OK)
                {
                    TicketForm objTicketForm = new JiaLiForm1_1();
                    ShowMessage = objTicketForm.ShowMessage;
                    Application.Run(objTicketForm);
                }
            }

        }

        static Action<object> ShowMessage;

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            ILogService objLogService= LogConfiguration.LogServiceProvider.CreateLogService();
            Exception objException = e.Exception;
            if (objException != null)
            {
                objLogService.WriteLog(objException);
                if (ShowMessage != null)
                {
                    ShowMessage(objException);
                }
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            ILogService objLogService = LogConfiguration.LogServiceProvider.CreateLogService();
            Exception objException = e.ExceptionObject as Exception;
            if (objException != null)
            {
                objLogService.WriteLog(objException);
                if (ShowMessage != null)
                {
                    ShowMessage(objException);
                }
            }
        }
    }
}
